﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;
using SalesSys.BLL.Sales;

namespace SalesSys.BLL
{
    public class PriceApprovalBLL : BaseBLL<PriceApproval, Guid>
    {
        public KeyValuePair<int, IEnumerable<PriceApproval>> GetList(string keyStr, int p1, int p2)
        {

            var where = Rep.Entities;
            if (!string.IsNullOrWhiteSpace(keyStr))
            {
                where = where.Where(p => p.Order.Customer.ShortName.Contains(keyStr) || p.Order.Creater.Name.Contains(keyStr));
            }

            var count = where.Count();
            var list = where.OrderBy(p => p.State).ThenByDescending(p => p.CreateTime).Paging(p1, p2).ToArray();
            return new KeyValuePair<int, IEnumerable<PriceApproval>>(count, list);
        }
        /// <summary>
        /// 申请议价
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="empId"></param>
        /// <param name="price"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public bool Apple(Guid orderId, Guid empId, decimal price, string remark)
        {
            var bll = BllFactory.GetBll<EmployeeBLL>();
            var emp = bll.Details(empId);
            var order = BllFactory.GetBll<OrderBLL>().Details(orderId);
            if (emp == null || order == null)
            {
                return false;
            }
            var apl = new PriceApproval() { Order = order, Applicant = emp, ApplyDiscount = price, CreateTime = DateTime.Now, ApplyRemark = remark };
            order.IsApproval = true;
            return Rep.Insert(apl) > 0;
        }
    }
}
